var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    open_gid: '',
    album_info: [],
    photos: [],
    page: 0,
    stopLoad: 0,
    show: false,
    imgUrls: [],
    current: 0
  },

  onLoad: function(options) {
    this.setData({
      id: options.id,
      is_inner: options.is_inner
    })
    this.setData({
      search: this.search.bind(this)
    })
  },

  createAlbum: function(data) {
    var _this = this;
    var open_id = wx.getStorageSync('open_id');
    app.request.requestPostApi({
      url: app.apiHost + 'album/create-album',
      params: {
        iv: data.iv,
        encryptedData: data.encryptedData,
        open_id
      },
      success: function(response) {
        if (response.code == 0) {
          _this.setData({
            id: response.data
          })
        } else {
          _this.createAlbum(data);
        }
      }
    })
  },

  onShow: function() {
    if (this.data.is_inner == 0) {
      var _this = this;
      wx.showShareMenu({
        withShareTicket: true
      })
      var obj = app.globalData.obj;
      if (obj.query != {}) {
        if (obj.shareTicket) {
          wx.getShareInfo({
            shareTicket: obj.shareTicket,
            success: function(res) {
              _this.createAlbum(res);
            }
          })
        }
        app.globalData.auto_go = 0;
      }
    }
    this.getAlbumInfo();
    this.getAlbumPhoto(this.data.page);
    this.viewPhoto();
  },

  onPullDownRefresh: function() {
    wx.showToast({
      icon: 'loading',
      title: '刷新中',
    })
    this.getAlbumInfo();
    this.getAlbumPhoto(0);
    wx.showToast({
      title: '刷新成功',
    })
  },
  // 上拉加载更多
  onReachBottom: function() {
    wx.showToast({
      icon: 'loading',
      title: '加载中',
    })
    if (this.data.stopLoad == 0) {
      this.getAlbumPhoto(this.data.page);

      wx.showToast({
        title: '加载成功',
      })
    } else {

      wx.showToast({
        title: '没有更多数据了',
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var _this = this;
    var imageUrl = "/images/1.jpg";
    var photos = this.data.photos;
    if (photos.length > 0 && photos[0].image.length > 0) {
      imageUrl = photos[0].image[0];
    }
    var path = 'pages/photos/photos?is_inner=0&id=' + this.data.id;
    return {
      title: '邀请您查看本群相册',
      path,
      imageUrl,
      success: function(res) {
        console.log(res)
      },
      complete: function(res) {
        if (res.errMsg == 'shareAppMessage:ok') {
          //分享为按钮转发
          if (_this.data.shareBtn) {
            //判断是否分享到群
            if (res.hasOwnProperty('shareTickets')) {
              console.log(res.shareTickets[0]);
              //分享到群
              _this.data.isshare = 1;
            } else {
              // 分享到个人
              _this.data.isshare = 0;
            }
          }
        } else {
          wx.showToast({
            title: '分享失败',
          })
          _this.data.isshare = 0;
        }
      },
    }
  },

  /**
   * 获取相册信息
   */
  getAlbumInfo: function() {
    var _this = this;
    if (this.data.id) {
      app.request.requestPostApi({
        url: app.apiHost + 'album/album-info',
        params: {
          id: _this.data.id
        },
        success: function(res) {
          if (res.code == -1) {
            _this.getAlbumInfo();
          } else {
            _this.setData({
              album_info: res.data
            })
          }
        }
      })
    } else {
      this.getAlbumInfo();
    }
  },

  /**
   * 获取相册照片
   */
  getAlbumPhoto: function(page,id = 0) {
    var _this = this;
    if (_this.data.id) {
      app.request.requestPostApi({
        url: app.apiHost + 'album/album-photo',
        params: {
          album_id: _this.data.id,
          page,
          id
        },
        success: function(res) {
          if (page == 0) {
            var photos = res.data;
          } else {
            var photos = _this.data.photos.concat(res.data);
          }
          var stopLoad = 0;
          if (res.data.length < 4) {
            stopLoad = 1;
          }
          _this.setData({
            photos,
            page: page + 1,
            stopLoad
          })
        }
      })
    } else {
      this.getAlbumPhoto();
    }
  },

  viewPhoto: function() {
    var _this = this;
    var open_id = wx.getStorageSync('open_id');
    if (this.data.id) {
      app.request.requestPostApi({
        url: app.apiHost + 'album/album-view',
        params: {
          id: _this.data.id,
          open_id
        },
        success: function(res) {
          if (res.code == -1) {
            _this.viewPhoto();
          }
        }
      })
    } else {
      this.viewPhoto();
    }
  },

  addImg: function() {
    this.setData({
      page: 0
    })
    wx.navigateTo({
      url: '/pages/add/add?id=' + this.data.id,
    })
    // wx.chooseImage({
    //   success: function(res) {
    //     console.log(res)
    //   },
    // })
    // wx.chooseMessageFile({
    //   count:9,
    //   type: 'video',
    //   success:function(res){
    //     console.log(res)
    //   }
    // })
  },
  showImage: function(e) {
    var imgUrls = this.data.photos[e.currentTarget.dataset.key].image
    this.setData({
      show: true,
      imgUrls,
      current: e.currentTarget.dataset.index
    })
  },

  del: function(e){
    wx.showModal({
      content: '是否确定删除',
      success: function(){
        app.request.requestPostApi({
          url: app.apiHost +  'album/del-photo',
          params: {
            id: e.currentTarget.dataset.id
          },
          success: function(res){
            if(res.code == 0){
              wx.showToast({
                title: '删除成功',
              })
            }
          }
        })
      }
    })
  },

  selectResult: function(res){
    var id = res.detail.item.id;
    this.getAlbumPhoto(0,id)
  },

  search: function (value) {
    return new Promise((resolve, reject) => {
      console.log(value);
      app.request.requestPostApi({
        url: app.apiHost+ 'album/search-photo',
        params:{
          key: value
        },
        success: function(res){
          resolve(res.data)
        }
      })
    })
  },

  clearSearch: function(){
    this.getAlbumPhoto(0)
  },
  clearInput: function () {
    this.getAlbumPhoto(0)
  }
})